//
//  ByCoreToolAlert.m
//  beibei_ios
//
//  Created by hebidu on 16/4/16.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByCoreToolAlert.h"

@interface ByCoreToolAlert() 

@end

@implementation ByCoreToolAlert

+ (UIWindow *)keywindow {
    
    return [UIApplication sharedApplication].keyWindow;
}

static CGFloat kDefaultDuration = 2.0f;

#pragma mark - ********************* 新版的提示框 *********************

+ (void)hudDefaultConfig:(MBProgressHUD *)hud {
    
    // 设置控件的颜色
    hud.contentColor = [UIColor colorWithWhite:1 alpha:1];
    // 设置mask背景
    hud.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    // 设置text的颜色
    hud.label.font = FONT(14);
    hud.label.numberOfLines = 0;
    // 设置detailText的颜色
    hud.detailsLabel.font = FONT(12);
    // 设置hud背景框的颜色
    hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.8];
    // 设置hud隐藏自动移除功能
    hud.removeFromSuperViewOnHide = YES;
    // 设置hud显示和隐藏动画
    hud.animationType = MBProgressHUDAnimationZoom;
}
/*
 tips:
 1.默认显示时长为2秒
 */
// 仅显示文字
+ (void)showOnlyText:(NSString *)text {
    
    [self showOnlyText:text hasMasker:NO];
}
+ (void)showOnlyText:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [self showOnlyText:text position:ByAlertPositionModeCenter];
    [MBProgressHUD HUDForView:[self keywindow]].userInteractionEnabled = hasMasker;
    [MBProgressHUD HUDForView:[self keywindow]].backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
}
+ (void)showOnlyText:(NSString *)text position:(ByAlertPositionMode)position {
    
    [self showOnlyText:text position:position duration:kDefaultDuration];
}
+ (void)showOnlyText:(NSString *)text position:(ByAlertPositionMode)position duration:(CGFloat)duration {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置模式
    hud.mode = MBProgressHUDModeText;
    // 设置文本
    hud.label.text = text;
    // 设置位置
    CGFloat offsetY = 0;
    switch (position) {
        case ByAlertPositionModeTop:
            offsetY = -MBProgressMaxOffset;
            break;
        case ByAlertPositionModeCenter:
            offsetY = 0;
            break;
        case ByAlertPositionModeBottom:
            offsetY = MBProgressMaxOffset;
            break;
        default:
            break;
    }
    hud.offset = CGPointMake(0.f, offsetY);
    
    [hud hideAnimated:YES afterDelay:duration];
}



// 加载中
+ (void)showLoadind:(NSString *)text {
    
    [self showLoadind:text hasMasker:NO];
}
+ (void)showLoadind:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置有否点击
    hud.userInteractionEnabled = hasMasker;
    // 设置背景色
    hud.backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
}
+ (void)showLoadind:(NSString *)text duration:(CGFloat)duration {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
    
    [hud hideAnimated:YES afterDelay:duration];
}




// 成功/失败
+ (void)showText:(NSString *)text state:(ByAlertStateMode)state {
    
    [self showText:text state:state duration:kDefaultDuration];
}
+ (void)showText:(NSString *)text state:(ByAlertStateMode)state duration:(CGFloat)duration {
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 设置自定义图片
    UIImage *image = nil;
    switch (state) {
        case ByAlertStateModeSuccess:
            image = [UIImage imageNamed:@"alert_success"];
            break;
        case ByAlertStateModeFailure:
            image = [UIImage imageNamed:@"alert_failure"];
            break;
        default:
            break;
    }
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.square = NO;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
    [hud hideAnimated:YES afterDelay:kDefaultDuration];
}
+ (void)showFailureWithText:(NSString *)text {
    
    [self showFailureWithText:text hasMasker:NO];
}
+ (void)showFailureWithText:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [self showText:text state:ByAlertStateModeFailure];
    [MBProgressHUD HUDForView:[self keywindow]].userInteractionEnabled = hasMasker;
    [MBProgressHUD HUDForView:[self keywindow]].backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
}
+ (void)showSuccessWithText:(NSString *)text {
    
    [self showSuccessWithText:text hasMasker:NO];
}
+ (void)showSuccessWithText:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [self showText:text state:ByAlertStateModeSuccess];
    [MBProgressHUD HUDForView:[self keywindow]].userInteractionEnabled = hasMasker;
    [MBProgressHUD HUDForView:[self keywindow]].backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
}

// 加载进度
+ (MBProgressHUD *)showProgress:(NSString *)text action:(void(^)(ByProgressUpload progress))actionBlock {
    
    return [self showProgress:text action:actionBlock hasMasker:NO];
}

+ (MBProgressHUD *)showProgress:(NSString *)text action:(void(^)(ByProgressUpload progress))actionBlock hasMasker:(BOOL)hasMasker {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置有否点击
    hud.userInteractionEnabled = hasMasker;
    hud.backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
    // 设置模式
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
    
    if (actionBlock) {
        
        ByProgressUpload progressBlock = ^ (CGFloat progress, BOOL competion) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD HUDForView:[self keywindow]].progress = progress;
            });
            
            if (competion) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [hud hideAnimated:YES];
                });
            }
        };
        actionBlock(progressBlock);
    }
    
    
    return hud;
}

// 隐藏
+ (void)hideHud {
    
    [[MBProgressHUD HUDForView:[self keywindow]] hideAnimated:YES];
}






@end
